package com.zzyk.main.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyk.main.configuration.exception.DeviceException;
import com.zzyk.main.mapper.DeviceMapper;
import com.zzyk.main.mapper.DeviceTypeMapper;
import com.zzyk.main.model.pojo.Device;
import com.zzyk.main.model.pojo.DeviceType;
import com.zzyk.main.model.vo.DeviceTypeVO;
import com.zzyk.main.service.DeviceTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceTypeServiceImpl implements DeviceTypeService {

    @Resource
    private DeviceTypeMapper deviceTypeMapper;
    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return deviceTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DeviceType record) {
        return deviceTypeMapper.insert(record);
    }

    @Override
    public int insertSelective(DeviceType record) {
        if (ObjectUtil.isNull(record.getRank())) {
            record.setRank(0);
        } else {
            switch (record.getRank()) {
                case 0:
                    record.setRank(1);
                    break;
                case 1:
                    record.setRank(2);
                    break;
            }
        }

        return deviceTypeMapper.insertSelective(record);
    }

    @Override
    public DeviceType selectByPrimaryKey(Integer id) {
        return deviceTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DeviceType record) throws DeviceException {
        if (ObjectUtil.isNotNull(record.getIsDelete())) {
            if (!deviceTypeMapper.selectByParentId(record.getId()).isEmpty()) {
                throw new DeviceException("该类型下存在子类型，请先删除子类型！");
            }
            if (!deviceMapper.selectByDeviceType(record.getId()).isEmpty()) {
                throw new DeviceException("该类型下存在设备，请先删除设备！");
            }
        }
        record.setUpdateTime(DateUtil.date());
        return deviceTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DeviceType record) {
        return deviceTypeMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<DeviceTypeVO> selectDeviceTypeTree() {
        return deviceTypeMapper.selectDeviceTypeTree();
    }

    @Override
    public List<DeviceType> selectByParentId(Integer parentId) {
        return deviceTypeMapper.selectByParentId(parentId);
    }

    @Override
    public List<DeviceType> selectByRank(Integer rank) {
        return deviceTypeMapper.selectAllByRank(rank);
    }

    @Override
    public Map<String, Boolean> uniqueName(Integer methodType, String title, Integer id) {
        Map<String, Boolean> map = new HashMap<>(8);
        if (methodType == 1) {
            if (ObjectUtil.isNull(deviceTypeMapper.selectAllByTitle(title))) {
                map.put("valid", true);
            } else {
                map.put("valid", false);
            }
        } else {
            if (ObjectUtil.isNull(deviceTypeMapper.selectAllByTitle(title))) {
                map.put("valid", true);
            } else {
              if(title.equals(deviceTypeMapper.selectByPrimaryKey(id).getTitle())){
                  map.put("valid", true);
              }else{
                  map.put("valid", false);
              }
            }
        }
        return map;
    }

    @Override
    public List<Device> getNoTypeDevice(String[] area) {
        return deviceMapper.selectNoTypeDevice(area);
    }

    @Override
    public List<DeviceType> getAllByUser(Integer enterprise, Integer[] deviceList) {
        return deviceTypeMapper.selectAllByUser(enterprise,deviceList);
    }

}




